//
//  ContactUsViewController.m
//  NewApp
//
//  Created by xueyan liu on 7/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ContactUsViewController.h"
#import "ChecksEndViewController.h"

@interface ContactUsViewController ()

@end

@implementation ContactUsViewController
@synthesize titleText;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_bg"]]];
    
    [scrollView setFrame:CGRectMake(0, 118, 320, 480)];
    [scrollView setContentSize:CGSizeMake(320, 530)];
    [scrollView setScrollEnabled:YES];
    
    fdEmail.delegate = self;
    fdCop.delegate = self;
    fdAddress.delegate = self;
    fdFax.delegate = self;
    fdTel.delegate = self;
    fdName.delegate = self;
    fdMemo.delegate = self;
    
    requestType = REQUEST_TYPE_INICONTACTUS;
    [self.engine contactusInfos];
    [self registerForKeyboardNotifications];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object{

    if (requestType == REQUEST_TYPE_INICONTACTUS) {
        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *result = (NSDictionary *) object;
            [result retain];
            
            self.titleLabel.text = [result objectForKey:@"top_title"];
            [result release];
        }
    }else {
        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *result = (NSDictionary *) object;
            [result retain];
            
            ChecksEndViewController *vctrl = [[ChecksEndViewController alloc] init];
            vctrl.titleText = [result objectForKey:@"top_title"];
            NSString *msg = [result objectForKey:@"msg"];
            vctrl.msg = msg;
            [self.navigationController pushViewController:vctrl animated:YES];
            [result release];
        }

    }

}

-(IBAction)confirm:(id)sender{
    
    if ((fdName.text == NULL) || ([fdName.text isEqualToString:@""])) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注意" message:@"個人姓名不能為空" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
        return;
    }
    
    if ((fdTel.text == NULL) || ([fdTel.text isEqualToString:@""])) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注意" message:@"聯絡電話不能為空" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
        return;
    }

    requestType = REQUEST_TYPE_CONTACTUS;
    [self.engine contactusWithName:fdName.text a2:fdCop.text a3:fdTel.text a4:fdFax.text a5:fdAddress.text a6:fdEmail.text a7:fdMemo.text];

}

#pragma mark - UITextField Delegate  
-(void)textFieldDidBeginEditing:(UITextField *)textField {  
    
    activeField = textField;  
}  

-(void)textFieldDidEndEditing:(UITextField *)textField {  
    activeField = nil;  
}  

-(BOOL)textFieldShouldReturn:(UITextField *)textField {  
    [textField resignFirstResponder];  
    return YES;  
}

// Call this method somewhere in your view controller setup code.  
- (void)registerForKeyboardNotifications {
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification {
    if (keyboardShown)
        return;
    
    NSDictionary* info = [aNotification userInfo];
    
    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    keyboardSize.height += 100;

    // Resize the scroll view (which is the root view of the window)
    CGRect viewFrame = [scrollView frame];
    viewFrame.size.height -= keyboardSize.height;
    scrollView.frame = viewFrame;
    
    // Scroll the active text field into view.
    CGRect textFieldRect = [activeField frame];

    [scrollView scrollRectToVisible:textFieldRect animated:YES];
    
    
    keyboardShown = YES;
}

// Called when the UIKeyboardDidHideNotification is sent
- (void)keyboardWasHidden:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    
    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    keyboardSize.height += 100;
    
    // Reset the height of the scroll view to its original value
    CGRect viewFrame = [scrollView frame];
    viewFrame.size.height += keyboardSize.height;
    scrollView.frame = viewFrame;
    
    keyboardShown = NO;
}


@end
